---扇形碰撞检测
---@class Battle.RectangleSelector:Battle.IRangeSelector
local RectangleSelector = Battle.CreateClass("RectangleSelector",Battle.IRangeSelector)
Battle.RectangleSelector = RectangleSelector

function RectangleSelector:ctor()
    self.pos = Vector3.New()
end
function RectangleSelector:Init(pos,forward,width,height,angle)
    self.width = width
    self.height = height
    self.pos:SetV(pos)
end
function RectangleSelector.GetType()
    return Battle.RangeSelectorType.Rect
end
function RectangleSelector:CalculateTargets(targetList)
    local entityList = Battle.ActorManager.GetActorList()
    for k,v in pairs(entityList) do
        if self:IsCircleIntersectionRect(self.pos,v.pos,v.radius) then
            table.insert(targetList,v)
        end
    end
end

---矩形是否和圆相交
function RectangleSelector:IsCircleIntersectionRect(pos,circlePoint,radius)
    local w = self.width/2
    local h = self.height/2
    local cx = circlePoint.x
    local cz = circlePoint.z
    local left = pos.x - w
    local top = pos.z + h
    local right = pos.x + w
    local bottom = pos.z - h
    --圆心
    local flag = self:IsPointInRect(cx,cz,left,top,right,bottom)
    if flag then return true end
    --4个点
    flag = self:IsPointInCircle(left,bottom ,cx,cz,radius)
    if flag then return true end
    flag = self:IsPointInCircle(right,bottom,cx,cz,radius)
    if flag then return true end
    flag = self:IsPointInCircle(right,top,cx,cz,radius)
    if flag then return true end
    flag = self:IsPointInCircle(left,top ,cx,cz,radius)
    if flag then return true end

    --边
    flag = (left - cx) <= radius and cz < top and cz > bottom
    if flag then return true end
    flag = (right - cx) <= radius and cz < top and cz > bottom
    if flag then return true end
    flag = (top - cz) <= radius and cx < right and cx > left
    if flag then return true end
    flag = (bottom - cz) <= radius and cx < right and cx > left
    if flag then return true end
    return flag
end
---点是否在圆内
function RectangleSelector:IsPointInCircle(pointX,pointY,circleX,circleY,radius)
    local x = pointX - circleX
    local z = pointY - circleY
    return x*x+z*z < radius * radius
end
function RectangleSelector:IsPointInRect(pointX,pointY,topLeftX,topLeftY,bottomRightX,bottomRightY)
    return pointX >= topLeftX and pointX <= bottomRightX and pointY <= topLeftY and pointY >= bottomRightY
end